﻿using System;
using System.Data.SqlClient;

namespace BwanaBank.BankOnline.WinUI.Window.Customer
{
    public partial class Account : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["customer"] == null && Session["customer"] == null)
            {
                Response.Redirect("~/");
            }
            var cn = new SqlConnection(@"Data Source=PAR-ROBHED-001\SQLEXPRESS;Initial Catalog=BWANABANk;Integrated Security=True");
            cn.Open();
            SqlCommand cmd = null;
            if (Request.Cookies["customer"] != null)
            {
                cmd = new SqlCommand("SELECT * FROM [User] WHERE customerNUm='" + Request.Cookies["customer"].Value + "'", cn);
            }
            else if (Session["customer"] != null)
            {
                cmd = new SqlCommand("SELECT * FROM [User] WHERE customerNum='" + Session["customer"] + "'", cn);
            }
            var dtr = cmd.ExecuteReader();
            if (dtr.Read())
            {
                var IDuser = (int)dtr["idUser"];
                dtr.Dispose();
                dtr.Close();
                cmd = new SqlCommand("SELECT * FROM [Account] WHERE idUser='" + IDuser + "'", cn);
                dtr = cmd.ExecuteReader();
                if (dtr.Read())
                {
                    numberAccount.Text = (string)dtr["idAccount"];
                    amountAccount.Text = (int)dtr["realBalance"] + " €";
                }
                dtr.Dispose();
                dtr.Close();
            }
            cn.Close();
        }
    }
}